package com.knirirr.beecountclassic;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class CountAdapter extends SimpleCursorAdapter 
{ 
  static final String[] FROM = { DbHelper.P_NAME, DbHelper.P_CREATED_AT };
  static final int[] TO = { R.id.textProject, R.id.textCreatedAt };
  //SimpleDateFormat df;
  DateFormat df;
  Typeface font;
  Date date;
  Context context;
  Boolean fontPref;

  // Constructor
  public CountAdapter(Context context, Cursor c, Boolean p) 
  {
    super(context, R.layout.row, c, FROM, TO);
    fontPref = p;
  }

  // This is where the actual binding of a cursor to view happens
  @Override
  public void bindView(View row, Context context, Cursor cursor) 
  {
    super.bindView(row, context, cursor);
    // create a fancy font and apply to both these views
    TextView textProject = (TextView) row.findViewById(R.id.textProject);
    // this is to get round the strange single quote problem 
    String editedText = (String) textProject.getText(); 
    textProject.setText(editedText.replaceAll("(^')|('$)","").replaceAll("''","'"));
    if (fontPref == true)
    {
      font = Typeface.createFromAsset(context.getAssets(), "village.ttf"); 
      textProject.setTypeface(font);
    }
    
    textProject.setTextColor(Color.WHITE);

    // Manually bind created at timestamp to its view
    long timestamp = cursor.getLong(cursor.getColumnIndex(DbHelper.P_CREATED_AT)); 
    TextView textCreatedAt = (TextView) row.findViewById(R.id.textCreatedAt);
    date = new Date(timestamp);
    //df = new SimpleDateFormat();
    df = SimpleDateFormat.getDateInstance();
    textCreatedAt.setText(df.format(date));
    if (fontPref == true)
    {
      textCreatedAt.setTypeface(font);
    }
    textCreatedAt.setTextColor(Color.WHITE);

  }
}
